package com.lnking.lnkingplace.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.lnking.lnkingplace.Util.LocalDateTimeUtil;
import com.lnking.lnkingplace.mapper.IsolationPlaceMapper;
import com.lnking.lnkingplace.mapper.IsolationWorkerMapper;
import com.lnking.lnkingplace.mapper.RecordMonitorMapper;
import com.lnking.lnkingplace.model.dto.record.monitor.MonitorIsolationImportDto;
import com.lnking.lnkingplace.model.dto.record.monitor.MonitorWorkerImportDto;
import com.lnking.lnkingplace.model.entity.IsolationPersonnel;
import com.lnking.lnkingplace.model.entity.IsolationWorker;
import com.lnking.lnkingplace.model.entity.RecordMonitor;
import com.lnking.lnkingplace.model.enums.DateFormat;
import com.lnking.lnkingplace.model.request.record.monitor.MonitorAddIsolationRequest;
import com.lnking.lnkingplace.model.request.record.monitor.MonitorAddRequest;
import com.lnking.lnkingplace.model.request.record.monitor.MonitorUpdateRequest;
import com.lnking.lnkingplace.service.IIsolationPersonnelService;
import com.lnking.lnkingplace.service.IIsolationWorkerService;
import com.lnking.lnkingplace.service.IRecordMonitorService;
import com.lnking.starter.export.utils.ImportOption;
import com.lnking.starter.export.utils.ImportUtils;
import com.lnking.starter.mybatis.service.impl.CustomServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhangqt
 * @since 2022-09-29
 */
@Service
public class RecordMonitorServiceImpl extends CustomServiceImpl<RecordMonitorMapper, RecordMonitor> implements IRecordMonitorService {

    @Resource
    IsolationPlaceMapper isolationPlaceMapper;
    @Resource
    IsolationWorkerMapper isolationWorkerMapper;

    @Autowired
    IIsolationPersonnelService isolationPersonnelService;

    @Autowired
    IIsolationWorkerService iIsolationWorkerService;


    public static final String  TYPE="工作人员";

    @Override
    public List<RecordMonitor> getInfo(String unionFlag) {
        return this.baseMapper.getInfo(unionFlag);
    }

    @Override
    public void addIsolation(List<MonitorAddIsolationRequest> params) {
        //获取主记录
        IsolationPersonnel data = isolationPersonnelService.getById(params.get(0).getUnionFlag());
        params.forEach(r ->{
            RecordMonitor info = new RecordMonitor();
            info.setPhone(data.getPhone());
            info.setPlaceId(data.getPlaceId());
            info.setBodyTemperature(r.getBodyTemperature());
            info.setName(r.getName());
            info.setUnionFlag(r.getUnionFlag());
            info.setCreateTime(LocalDateTime.now());
            info.setThermometryTime(LocalDateTime.now());
            info.setRoom(data.getRoomNumber());
            info.setSex(data.getSex());
            info.setSymptom(r.getSymptom());
            info.setCheckTime(LocalDateTimeUtil.stringToLocalDateTime(r.getCheckTime(),DateFormat.LOCAL_DATE_TIME));
            info.setResult(r.getResult());
            this.baseMapper.insert(info);
        });
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importIsolation(MultipartFile file) throws Exception {
        ImportOption option = new ImportOption.Builder().build();
        List<MonitorIsolationImportDto> result =  ImportUtils.importExcel(file, MonitorIsolationImportDto.class,option);
        result.forEach(r ->{
            if(StringUtils.isNotBlank(r.getPlaceName())){
                RecordMonitor info = new RecordMonitor();
                BeanUtils.copyProperties(r,info);
                info.setCreateTime(LocalDateTime.now());
                info.setThermometryTime(LocalDateTimeUtil.stringToLocalDateTime(r.getThermometryTime(), DateFormat.LOCAL_DATE_TIME));
                info.setCheckTime(LocalDateTimeUtil.stringToLocalDateTime(r.getCheckTime(),DateFormat.LOCAL_DATE_TIME));
                info.setBodyTemperature(Float.parseFloat(r.getBodyTemperature()));
                info.setPersonnelType("隔离人员");
                //查询placeId
                String placeId = isolationPlaceMapper.getPlaceId(r.getPlaceName());
                Assert.isTrue(StringUtils.isNotBlank(placeId), "该场所不存在："+"【"+r.getPlaceName()+"】");
                info.setPlaceId(placeId);

                Integer check = check(r.getName(), r.getThermometryTime());
                Assert.notNull(info.getPhone(),"请输入手机号");
                if(check == 0){
                    this.baseMapper.insert(info);
                }
            }
        });
    }

    public Integer check(String name,String date){
        LambdaQueryWrapper<RecordMonitor> query = new LambdaQueryWrapper<>();
        query.eq(RecordMonitor::getName,name);
        query.eq(RecordMonitor::getThermometryTime,LocalDateTimeUtil.stringToLocalDateTime(date,DateFormat.LOCAL_DATE_TIME));
        return this.baseMapper.selectCount(query);
    }

    @Override
    public void importWorker(MultipartFile file) throws Exception {
        ImportOption option = new ImportOption.Builder().build();
        List<MonitorWorkerImportDto> result =  ImportUtils.importExcel(file, MonitorWorkerImportDto.class,option);
        result.forEach(r ->{
            if(StringUtils.isNotBlank(r.getPlaceName())){
                RecordMonitor info = new RecordMonitor();
                BeanUtils.copyProperties(r,info);
                info.setCreateTime(LocalDateTime.now());
                info.setThermometryTime(LocalDateTimeUtil.stringToLocalDateTime(r.getThermometryTime(),DateFormat.LOCAL_DATE_TIME));
                info.setCheckTime(LocalDateTimeUtil.stringToLocalDateTime(r.getCheckTime(),DateFormat.LOCAL_DATE_TIME));
                info.setBodyTemperature(Float.parseFloat(r.getBodyTemperature()));
                info.setPersonnelType("工作人员");
                Assert.notNull(info.getPhone(),"请输入手机号");
                //查询placeId
                String placeId = isolationPlaceMapper.getPlaceId(r.getPlaceName());
                Assert.isTrue(StringUtils.isNotBlank(placeId), "该场所不存在："+"【"+r.getPlaceName()+"】");
                info.setPlaceId(placeId);
                this.baseMapper.insert(info);
            }
        });
    }

    @Override
    public void add(MonitorAddRequest params) {
        RecordMonitor info = new RecordMonitor();
        BeanUtils.copyProperties(params,info);
        info.setCreateTime(LocalDateTime.now());
        info.setPlaceId(isolationPlaceMapper.getPlaceId(params.getPlaceName()));
        this.baseMapper.insert(info);
    }


    @Override
    public void update(MonitorUpdateRequest params) {
        RecordMonitor info = new RecordMonitor();
        BeanUtils.copyProperties(params,info);
        info.setPlaceId(isolationPlaceMapper.getPlaceId(params.getPlaceName()));
        this.baseMapper.updateById(info);
    }


}
